(function () {

    "use strict";

    var api = {
        "addrListsUrl": '/api/address/addrLists'
    };
    sm.controller('wx-center-address-select-controller', ['$scope', '$http', 'Modal', 'Toast', function ($scope, $http, Modal, Toast){

        $scope.addrLists = [];

        function formatAddr(addrLists) {
            angular.forEach(addrLists, function (val) {
                $scope.addrLists.push({
                    addressId: val.addressId,
                    name: val.name,
                    phone: val.phone,
                    detail: val.province + ' ' + val.city + ' ' + val.area + ' ' + val.address,
                    default: val.default
                });
            });
        }

        $http.get(api.addrListsUrl).success(function (res) {
            if (res.state) {
                formatAddr(res.data);
            }
        });

        /**
         * 选择地址
         * @param addr
         * @returns {boolean}
         */
        $scope.selected = function (addr) {
            if (!addr.addressId) {
                return false;
            }

            var orderId = GetQueryString('orderId');
            var url = '/order/confirm?addressId=' + addr.addressId;
            if (orderId) {
                url = '/order/pay?orderId=' + orderId + '&addressId=' + addr.addressId;
            }

            location.href = url;
        };
    }]);
})();

